This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
market_basket <- list(
c("apple","beer","rice","meat"),
c("apple","beer","rice"),
c("apple","beer"),
c("apple","pear"),
c("milk","beer","rice","meat"),
c("apple","beer","rice"),
c("milk","beer"),
c("milk","pear")
)
length(market_basket)
[1] 8
library(arules)
library(arulesViz)
names(market_basket) <- paste("T",c(1:8),sep=" ")
dim(market_basket)
NULL
trans <- as(market_basket,"transactions")
dim(trans)
[1] 8 6
itemLabels(trans)
[1] "apple" "beer" "meat" "milk" "pear" "rice"
summary(trans)
transactions as itemMatrix in sparse format with
8 rows (elements/itemsets/transactions) and
6 columns (items) and a density of 0.4583333
most frequent items:
beer apple rice milk meat (Other)
6 5 4 3 2 2
element (itemset/transaction) length distribution:
sizes
2 3 4
4 2 2
Min. 1st Qu. Median Mean 3rd Qu. Max.
2.00 2.00 2.50 2.75 3.25 4.00
includes extended item information - examples:
includes extended transaction information - examples:
image(trans)
itemFrequencyPlot(trans,topN=10,cex.names=1)
rules <- apriori(trans,parameter = list(supp=0.3,conf=0.5,maxlen=10,target="rules"))
Apriori
Parameter specification:
Algorithmic control:
Absolute minimum support count: 2
set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[6 item(s), 8 transaction(s)] done [0.00s].
sorting and recoding items ... [4 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 3 done [0.00s].
writing ... [12 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].
summary(rules)
set of 12 rules
rule length distribution (lhs + rhs):sizes
1 2 3
3 6 3
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 1.75 2.00 2.00 2.25 3.00
summary of quality measures:
support confidence coverage lift
Min. :0.3750 Min. :0.5000 Min. :0.3750 Min. :1.000
1st Qu.:0.3750 1st Qu.:0.6562 1st Qu.:0.5000 1st Qu.:1.050
Median :0.5000 Median :0.7500 Median :0.6250 Median :1.200
Mean :0.4792 Mean :0.7382 Mean :0.6771 Mean :1.186
3rd Qu.:0.5000 3rd Qu.:0.7625 3rd Qu.:0.8125 3rd Qu.:1.333
Max. :0.7500 Max. :1.0000 Max. :1.0000 Max. :1.500
count
Min. :3.000
1st Qu.:3.000
Median :4.000
Mean :3.833
3rd Qu.:4.000
Max. :6.000
mining info:
inspect(rules)
beer_rules_rhs <- apriori(trans,parameter=list(supp=0.3,conf=0.5,maxlen=10,minlen=2),
appearance=list(default="lhs",rhs="beer"))
Apriori
Parameter specification:
Algorithmic control:
Absolute minimum support count: 2
set item appearances ...[1 item(s)] done [0.00s].
set transactions ...[6 item(s), 8 transaction(s)] done [0.00s].
sorting and recoding items ... [4 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 3 done [0.00s].
writing ... [3 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].
inspect(beer_rules_rhs)
beer_rules_lhs <- apriori(trans,parameter=list(supp=0.3,conf=0.5,maxlen=10,minlen=2),
appearance=list(default="rhs",lhs="beer"))
Apriori
Parameter specification:
Algorithmic control:
Absolute minimum support count: 2
set item appearances ...[1 item(s)] done [0.00s].
set transactions ...[6 item(s), 8 transaction(s)] done [0.00s].
sorting and recoding items ... [4 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 done [0.00s].
writing ... [2 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].
inspect(beer_rules_lhs)
plot(rules)
To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.
plot(rules,engine="plotly")
To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.
Registered S3 method overwritten by 'data.table':
method from
print.data.table
Registered S3 methods overwritten by 'htmltools':
method from
print.html tools:rstudio
print.shiny.tag tools:rstudio
print.shiny.tag.list tools:rstudio
Registered S3 method overwritten by 'htmlwidgets':
method from
print.htmlwidget tools:rstudio
subrules <- head(rules,n=10,by="confidence")
plot(subrules,method="graph",engine="htmlwidget")
plot(subrules,method="paracoord")
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.